package com.softqin.washer.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.softqin.washer.R;
import com.softqin.washer.activity.MainActivity;
import com.softqin.washer.common.WasherApp;
/**
 * 

* @ClassName: BottomControlFragment 

* @date 2014-12-27 11:11:54
 */
public class BottomControlFragment extends Fragment {
	private final static String TAG = "BottomControlFragment";
	private final static int COLOR_SELECT_ID = Color.rgb(0, 139, 250);
	private MainActivity mMainActivity;
	public Fragment[] mArrFragments;
	private View mView_fragment_bottomcontrol;
	private RelativeLayout mRl_home;
	private RelativeLayout mRl_order;
	private RelativeLayout mRl_myself;

	private ImageView mImgView_home;
	private ImageView mImgView_order;
	private ImageView mImgView_myself;

	private TextView mTxtView_home;
	private TextView mTxtView_order;
	private TextView mTxtView_myself;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		mMainActivity = (MainActivity) getActivity();
		mView_fragment_bottomcontrol = inflater.inflate(
				R.layout.fragment_bottom_control, container, false);
		Init();
		return mView_fragment_bottomcontrol;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}
	private void Init() {
		mRl_home = (RelativeLayout) mView_fragment_bottomcontrol
				.findViewById(R.id.rl_bottom_control_home);
		mRl_order = (RelativeLayout) mView_fragment_bottomcontrol
				.findViewById(R.id.rl_bottom_control_order);
		mRl_myself = (RelativeLayout) mView_fragment_bottomcontrol
				.findViewById(R.id.rl_bottom_control_myself);
		
		mImgView_home = (ImageView) mRl_home
				.findViewById(R.id.imgView_bottom_control_home);
		mImgView_order = (ImageView) mRl_order
				.findViewById(R.id.imgView_bottom_control_order);
		mImgView_myself = (ImageView) mRl_myself
				.findViewById(R.id.imgView_bottom_control_myself);

		mTxtView_home = (TextView) mRl_home
				.findViewById(R.id.txtView_bottom_control_home);
		mTxtView_order = (TextView) mRl_order
				.findViewById(R.id.txtView_bottom_control_order);
		mTxtView_myself = (TextView) mRl_myself
				.findViewById(R.id.txtView_bottom_control_myself);

		mRl_home.setOnClickListener(new OnClickListenerImpl());
		mRl_order.setOnClickListener(new OnClickListenerImpl());
		mRl_myself.setOnClickListener(new OnClickListenerImpl());
	}

	private void bottomReset() {
		mImgView_home
				.setBackgroundResource(R.drawable.indexbottomiconhome);
		mTxtView_home.setTextColor(R.color.gray);

		mImgView_order
				.setBackgroundResource(R.drawable.indexbottomiconorder);
		mTxtView_order.setTextColor(R.color.gray);

		mImgView_myself
				.setBackgroundResource(R.drawable.indexbottomiconmy);
		mTxtView_myself.setTextColor(R.color.gray);

	}

	private void setFragment(int whichIsSelect) {
		mArrFragments = new Fragment[4];
		mArrFragments[0] = getFragmentManager().findFragmentById(
				R.id.bottomControlFragment);
		mArrFragments[1] = getFragmentManager()
				.findFragmentById(R.id.homeFragment);
		mArrFragments[2] = getFragmentManager()
				.findFragmentById(R.id.orderFragment);
		mArrFragments[3] = getFragmentManager()
				.findFragmentById(R.id.myselfFragment);
		mArrFragments[3].setUserVisibleHint(true);
		
		getFragmentManager().beginTransaction().hide(mArrFragments[1])
				.hide(mArrFragments[2]).hide(mArrFragments[3])
				.show(mArrFragments[whichIsSelect]).commit();
		getFragmentManager().beginTransaction().show(mArrFragments[whichIsSelect])
				.show(mArrFragments[0]);
		
		WasherApp.getInstance().mIHomepageBottomId = whichIsSelect;
	}
	
	private class OnClickListenerImpl implements OnClickListener {

		@Override
		public void onClick(View view) {
			bottomReset();
			switch (view.getId()) {
			case R.id.rl_bottom_control_home:
				mImgView_home
						.setBackgroundResource(R.drawable.indexbottomiconhomestate);
				mTxtView_home.setTextColor(COLOR_SELECT_ID);
				mMainActivity.setTiTleBar("南京",getString(R.string.app_name));
				setFragment(1);
				break;
			case R.id.rl_bottom_control_order:
				mImgView_order
						.setBackgroundResource(R.drawable.indexbottomiconorderstate);
				mTxtView_order.setTextColor(COLOR_SELECT_ID);
				mMainActivity.setTiTleBar("",getString(R.string.my_order_title));
				setFragment(2);
				break;
			case R.id.rl_bottom_control_myself:
				mImgView_myself
						.setBackgroundResource(R.drawable.indexbottomiconmystate);
				mTxtView_myself.setTextColor(COLOR_SELECT_ID);
				mMainActivity.setTiTleBar("",getString(R.string.bottom_control_myself));
				setFragment(3);
				break;
			}
		}

	}
}
